package leave.dataaccess.mysql;

import java.sql.*;

import leave.dataaccess.AdminDAO;
import leave.exception.DataAccessException;
import leave.model.Administrator;
import leave.model.impl.mysql.AdministratorImpl;

public class AdminDAOImpl implements AdminDAO {

	private Connection FactoryConnection;

	
	public AdminDAOImpl(Connection factoryConnection) {
		super();
		FactoryConnection = factoryConnection;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see leave.dataaccess.AdminDAO#authenticate(int, java.lang.String) Check
	 * if the user has enter correct username and password.
	 */
	@Override
	public boolean authenticate(int userName, String password) throws DataAccessException {
		// TODO Add code here
		String selectSql = "select name from employee where employeeid = userName and password='"
				+ password.trim() + "';";
		boolean result;
		Statement st = null;
		ResultSet rs = null;
		try {
			st = FactoryConnection.createStatement();
			rs = st.executeQuery(selectSql);
			result = ((rs.getRow() == 1) ? true : false);
			/*
			 * if (rs.getRow()==1) return true; else return false; //
			 */
			// while(rs.next())
			// {
			// emplyee.setName(rs.getString("name"));
			//
			// }
			//
		} catch (SQLException e) {

			throw new DataAccessException(e);
		} finally {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new DataAccessException(e);
			}
		}
		return result;
	}

	@Override
	public Administrator createAdministrator() {
		return new AdministratorImpl();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see leave.dataaccess.AdminDAO#addAdmin(leave.model.Administrator) Add
	 * administrator to database.
	 */
	@Override
	public void addAdmin(Administrator admin) throws DataAccessException {

		String insertSql = "insert into administrator values (?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = FactoryConnection.prepareStatement(insertSql);
			ps.setInt(1, admin.getUserId());
			ps.setString(2, admin.getName());
			ps.setString(3, admin.getPassword());

			ps.executeUpdate();
		} catch (SQLException e) {
			throw new DataAccessException("Unable to carry out transaction", e);
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new DataAccessException(
						"Unable to carry out transaction.", e);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see leave.dataaccess.AdminDAO#updateAdmin(leave.model.Administrator)
	 * Update administrator to database.
	 */
	@Override
	public void updateAdmin(Administrator admin) throws DataAccessException {

		String updateSql = "update administrator set name=?,password=? where adminid=?";

		PreparedStatement ps = null;
		try {
			ps = FactoryConnection.prepareStatement(updateSql);
			ps.setString(1, admin.getName());
			ps.setString(2, admin.getPassword());
			ps.setInt(3, admin.getUserId());
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new DataAccessException("Unable to carry out transaction.", e);
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				throw new DataAccessException(
						"Unable to carry out transaction.", e);
			}

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see leave.dataaccess.AdminDAO#removeAdmin(leave.model.Administrator)
	 * Remove administrator from database.
	 */
	@Override
	public void removeAdmin(Administrator admin) throws DataAccessException {
		String deleteSql = "delete from administrator where adminid="
				+ admin.getUserId();

		Statement st = null;

		try {
			st = FactoryConnection.createStatement();
			st.executeUpdate(deleteSql);
			
			admin = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DataAccessException("Unable to carry out transaction.", e);
		} finally {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new DataAccessException(
						"Unable to carry out transaction.", e);
			}
		}
	}
}
